<template>
  <div>
    <dami-dialog @confirm="confirm" @cancel="cancel">
      <upload ref='upload' :action-url='imgUploadPath' :multiple='uploadType'></upload>
    </dami-dialog>
  </div>
</template>

<script type="text/javascript">
import upload from  '../../../component/upload';
import SYS_Product_update from  '../../../API/SYS_Product_update.js';
import SYS_Product_single from  '../../../API/SYS_Product_single.js';
import img_get from  '../../../API/img_get.js';
import img_update from  '../../../API/img_update.js';

const config = require('../../../../config');
const imgUploadPath = !!process.env  ? config.dev.imgUploadPath : config.build.imgUploadPath;
const imgPath = !!process.env  ? config.dev.imgPath : config.build.imgPath;

  export default {
    data(){
      return {
        imgUploadPath:imgUploadPath,
        Cover:'',
        DetailImgId:'',
        BannerId:''
      }
    },
    props:['edit-id','view-state','type'],//1是首图 2是详情 3是banner
    mounted:function(){
      this.getProMes();
    },
    watch:{
      viewState(val){
        if(!!val){
          this.getProMes();
        }
      }
    },
    computed:{
      uploadType(){
        if(this.type == "1"){
          return false;
        }
        if(this.type == "2"){
          return true;
        }
        if(this.type == "3"){
          return true;
        }
      }
    },
    components:{
      upload
    },
    methods:{
      initCover(){
        let parm = {
          LinkId:this.Cover
        };
        img_get(parm).then(data => {
          console.log("........")
          console.log(data)
          if(data.length > 0){
            this.$refs.upload.initData([imgPath+data[0].Path])
          }
        }).catch(message => {
          console.log("img_get异常：",message)
          this.$Message({
              message
          });
        });
      },
      initDetailImgId(){
        let parm = {
          LinkId:this.DetailImgId
        };
        img_get(parm).then(data => {
          if(data.length > 0){
            let imgPathList = [];
            for(let i = 0; i < data.length; i++){
              imgPathList.push(imgPath+data[i].Path);
            }
            this.$refs.upload.initData(imgPathList)
          }
        }).catch(message => {
          this.$Message({
              message
          });
        });
      },
      initBannerId(){
        let parm = {
          LinkId:this.BannerId
        };
        img_get(parm).then(data => {
          if(data.length > 0){
            let imgPathList = [];
            for(let i = 0; i < data.length; i++){
              imgPathList.push(imgPath+data[i].Path);
            }
            this.$refs.upload.initData(imgPathList)
          }
        }).catch(message => {
          this.$Message({
              message
          });
        });
      },
      getProMes(){
        if(!this.editId){
          this.$Message({
              message:"请传递商品ID"
          });
          return;
        }
        SYS_Product_single({Id:this.editId}).then(data => {
          this.Cover = data.Cover;
          this.DetailImgId = data.DetailImgId;
          this.BannerId = data.BannerId;

          if(this.type == "1" && this.Cover != ""){
            this.initCover();
          }
          if(this.type == "2" && this.DetailImgId != ""){
            this.initDetailImgId();
          }
          if(this.type == "3" && this.BannerId != ""){
            this.initBannerId();
          }

        }).catch(message => {
          console.log("SYS_Product_single异常:",message)
          this.$Message({
              message
          });
        });
      },
      confirmImg(Linkid,img){
        let parm = {
          Linkid:Linkid,
          imgPath:img
        };
        img_update(parm).then(data => {
          this.$Message({
              message:"操作成功"
          });
          this.$emit("confirm");
        }).catch(message => {
          this.$Message({
              message
          });
        });
      },
      confirm(){
        this.$refs.upload.upload(data => {

          if(data.res == "0"){
            this.$Message({
                message:data.mes
            });
            return;
          }
          if(data.res == "-1"){
            this.$Message({
                message:data.mes
            });
            return;
          }

          if(this.type == "1"){
            let imgList = data.imgList;
            this.confirmImg(this.Cover,imgList[0].value);
          }
          if(this.type == "2"){
            let imgList = data.imgList;
            let DetailImgId = "";
            for(let i = 0; i < imgList.length; i++){
              DetailImgId = DetailImgId + imgList[i].value + ",";
            }

            this.confirmImg(this.DetailImgId,DetailImgId);
          }
          if(this.type == "3"){
            let  BannerId = "";
            let imgList = data.imgList;
            for(let i = 0; i < imgList.length; i++){
              BannerId = BannerId + imgList[i].value + ",";
            }

            this.confirmImg(this.BannerId,BannerId);
          }

        });

      },
      cancel(){
        this.$emit("cancel");
        this.$refs.upload.reset();
      }
    }
  }
</script>
